package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "菜品接口")
@RequestMapping("/dish")
@RestController
public class DishController {
    
    @Autowired
    private DishService ds;
    
    /**
     * 根据分类的id，获取菜品数据
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId" , value = "分类id") ,
            @ApiImplicitParam(name="status", value = "状态")
    })
    @ApiOperation(value = "查询菜品数据")
    public R list(long categoryId , Integer status){
    
        try {
            //1. 调用service
//            List<Dish> list = ds.findByCategoryId(categoryId , status);
            List<DishDto> list = ds.findByCategoryId(categoryId , status);
        
            return R.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败！");
        }
    
    }
    
    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        
        int row = ds.update(dishDto);
        if(row >0 ){
            return R.success("更新成功!");
        }else{
            return R.error("更新失败！");
        }
    
    }
    
    /**
     * 分页查询菜品
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page" , value = "请求页码"),
            @ApiImplicitParam(name = "pageSize" , value = "每页显示条数"),
            @ApiImplicitParam(name = "name" , value = "查询菜品名称")
    })
    public R findPage(PageParam pageParam){
    
        try {
            //1. 调用service
            IPage<DishDto> page = ds.findPage(pageParam);
        
            //2. 返回数据
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败！");
        }
    }
    
    
    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        System.out.println("dishDto = " + dishDto);
    
        //1. 调用service
        int row = ds.add(dishDto);
    
        //2. 返回
        if(row >0 ){
            return R.success("添加菜品成功！");
        }else{
            return R.error("添加菜品失败！");
        }
    
    }
}
